﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JamesThew.Dashboard
{
    public partial class Statistics : System.Web.UI.Page
    {
        public Analytics an;
        
        public String MonthyRevenue;
        public String YearlyRevenue;
        public String[] PostData;
        public String[] CommentData;

        public List<String[]> GeneralPaymentData;
        public List<String[]> MonthyPaymentData;
        public List<String[]> YearlyPaymentData;
        public List<String[]> CanceledPaymentData;

        public String GeneralTableData;
        public String MonthyTableData;
        public String YearlyTableData;
        public String CanceledTableData;


        public String GroupBy;

        protected void Page_Load(object sender, EventArgs e)
        {
            /* Group the table data by date or month */
            String GroupBy = Request.QueryString["group"];
            if (GroupBy == null) GroupBy = "";
            this.GroupBy = GroupBy;
            
            /* Select Date Created */
            String FromDate = Request.QueryString["fromdate"];
            int DateMin = JamesThew.Includes.Core.ParseInt(FromDate);
            String ToDate = Request.QueryString["todate"];
            int DateMax = JamesThew.Includes.Core.ParseInt(ToDate);
            String FromMonth = Request.QueryString["frommonth"];
            int MonthMin = JamesThew.Includes.Core.ParseInt(FromMonth);
            String ToMonth = Request.QueryString["tomonth"];
            int MonthMax = JamesThew.Includes.Core.ParseInt(ToMonth);

            /* Select the payment type */
            String PaymentType = Request.QueryString["paymenttype"];
            if (PaymentType == "") PaymentType = null;

            this.an = new Analytics();

            /* Generate Yearly table */
            this.YearlyPaymentData = an.AdvancedGetAnalytics(2012, MonthMin, MonthMax, DateMin, DateMax, GroupBy, "yearly", PaymentType, "approved");
            this.YearlyTableData = this.GenerateTableData(YearlyPaymentData, "yearly", PaymentType, "approved");

            /* Generate Monthy table */
            this.MonthyPaymentData = an.AdvancedGetAnalytics(2012, MonthMin, MonthMax, DateMin, DateMax, GroupBy, "monthy", PaymentType, "approved");
            this.MonthyTableData = this.GenerateTableData(MonthyPaymentData, "monthy", PaymentType, "approved");

            this.GeneralPaymentData = an.GeneralPaymentAnalytics(2012, MonthMin, MonthMax, DateMin, DateMax, null, null, PaymentType, "approved");
            this.GeneralTableData = this.GenGeneralTableData(GeneralPaymentData, MonthMin, MonthMax, DateMin, DateMax, PaymentType);


            List<String[]> ls = an.GetPaymentInfo();

            String Output = "";
            foreach (String[] S in ls) {
               Output += "['" + S[2] + "', " + S[3] + ", " + S[4] + "],";
            }

            this.YearlyRevenue = Output;


            List<String[]> listMonthy = an.GetMonthyPaymentInfo();
            String MonthyOutput = "";
            foreach (String[] M in listMonthy) {
                MonthyOutput += "['" + M[2] + "', " + M[3] + ", " + M[4] + "],";
            }
            this.MonthyRevenue = MonthyOutput;

            List<String[]> ListPostData = an.GetPostsData();
            String[] PostsDataArr = new String[5];

            for (int i = 0; i <= 4; i++)
            {
                PostsDataArr[i] = ListPostData[i][1].ToString();
            }
            this.PostData = PostsDataArr;


            List<String[]> ListCommentData = an.GetCommentsData();
            String[] CommentDataArr = new String[4];
            for (int i = 0; i <= 3; i++) {
                CommentDataArr[i] = ListCommentData[i][1].ToString();
            }
            this.CommentData = CommentDataArr;

        }

        public String GenerateTableData(List<String[]> PaymentData, String PaymentPackage, String PaymentMethod, String PaymentStatus) {
            String output = "";

            foreach (String[] S in PaymentData) {
                output += "<tr><td>" + S[0] + "</td>";
                output += "<td>" + S[1] + "</td>";
                if (this.GroupBy == "date")
                {
                    output += "<td>" + S[4] + "</td>";
                }
                output += "<td>" + S[2] + "</td>";
                if (this.GroupBy != "date")
                {
                    output += "<td><a href='Payments.aspx?year=" + S[0] + "&monthmin=" + S[1] + "&package=" + PaymentPackage + "&method=" + PaymentMethod + "&status=" + PaymentStatus + "'>" + S[3] + "</a></td>";
                }
                else
                {
                    output += "<td><a href='Payments.aspx?year=" + S[0] + "&monthmin=" + S[1] + "&date="+S[4]+"&package=" + PaymentPackage + "&method=" + PaymentMethod + "&status=" + PaymentStatus + "'>" + S[3] + "</a></td>";
                }
                output += "</tr>";
            }
            return output;
        }

        public String GenGeneralTableData(List<String[]> TableData, int MonthMin, int MonthMax, int DateMin, int DateMax, String PaymentType) {
            String output = "";

            int TotalRegistered = 0, TotalRevenue = 0;

            foreach( String[] S in TableData ){
                output += "<tr><td>" + S[0] + "</td><td>" + S[1] + "</td><td><a href='Payments.aspx?monthmin=" + MonthMin + "&monthmax=" + MonthMax + "&datemin=" + DateMin + "&datemax=" + DateMax + "&status=approved&package="+S[0]+"&method="+PaymentType+"'>" + S[2] + "</a></td></tr>";
                TotalRegistered = TotalRegistered + JamesThew.Includes.Core.ParseInt(S[1]);
                TotalRevenue = TotalRevenue + JamesThew.Includes.Core.ParseInt(S[2]);
            }
            output += "<tr><td>Total</td><td>" + TotalRegistered + "</td><td><a href='Payments.aspx?monthmin=" + MonthMin + "&monthmax=" + MonthMax + "&datemin=" + DateMin + "&datemax=" + DateMax + "&status=approved&method="+PaymentType+"'>" + TotalRevenue + "</a></td></tr>";
            return output;
        }
    }
}